<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            蓝牙连接
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        </br>

        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:100px;">
            <table class="table" style='width:100%;height: 200px' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'></td>
                        <td algin='center'>UUID</td>
                        <!-- <td algin='center'>rssi</td> -->
                        <td algin='center'>蓝牙名称</td>
                        <!-- <td algin='center' style="display:none;">manufacturerData</td> -->
                        <td algin='center'>连接</td>
                        <td style="display:none;">id</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    <!-- <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer> -->

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-btn aui-btn-info aui-btn-block"
            style="width:60%;margin-left:20%;font-size: 12px;font-weight: bold;" onclick="createUqcmChkBill()">扫描蓝牙
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    // var idSelector = "";

    //初始化查询仓库下拉框
    apiready = function () {

    }

    function doscan() {
        var ble = api.require('ble');
        ble.initManager(function (ret) {
            if (ret.state == "poweredOn") {
                api.alert({ msg: "初始化成功" });

                ble.scan({
                    // serviceUUIDs: ['', '']
                }, function (ret) {
                    if (ret.status) {
                        alert('开始扫描');
                        ble.getPeripheral(function (ret) {
                            if (ret) {
                                api.alert({ msg: JSON.stringify(ret) });
                            }
                        });
                    }
                });
            }
        });
    }

    function clickBle(uuid) {
       api.openWin({
            name: 'connect_Bluetooth',
            url: 'connect_Bluetooth.html',
            pageParam: {
                uuid: uuid,
            }
       });


    }


    //很指定的外国设备 WUID 及其服务 WUID 获族外围设备的有特《Characteristic)
    function discoverCharacteristics(services) {
        let _this = this
        for (let item of services) {
            this.ble.discoverCharacteristics({
                serviceUuID: item, //成务WID
                peripheraluUID: "3A:CA:84:8F:B7:CC" //蓝于外国设务的 UUID
            }, function (ret) {
                if (ret) (
                    console.log("获职该设备某一服务里所有的特征:", JSON.stringify(ret.characteristics))

                    // for (var i =0;i<2;i++){

                    // }

                    // for (let _obj of ret.characteristics) {
                    //     if (_obj.properties == "22") {
                    //     // model.propertiesuUID = obj.uuid //征的UID
                    //     // model.servicewUID = item //双务的UUID
                    //     // this.writeBLECharacteristicValue(ORDERSystemTime)
                    //     }
                    // }
                );
            });
        }

    }


    //一键发货
    function createUqcmChkBill() {
        var ble = api.require('ble');
        ble.initManager({ single: true }, function (ret) {
            if (ret.state == "poweredOn") {
                // api.alert({ msg: "初始化成功" });
                api.showProgress({
                    title: '扫描蓝牙中...',
                    modal: true
                });
                var html = "";
                setTimeout(
                    ble.scan({
                        //serviceUUIDs: ['', '']
                        clean: false
                    }, function (ret) {
                        if (ret.status) {

                            //alert('开始扫描');

                            setTimeout(
                                ble.getPeripheral(function (ret) {
                                    if (ret) {



                                        //api.alert({ msg: JSON.stringify(ret) });
                                        //api.alert({ msg: JSON.stringify(ret.peripherals) });

                                        var data = JSON.stringify(ret);
                                        var myobj = eval("(" + data + ")");
                                        //var list1 = JSON.stringify(ret).peripherals;
                                        var list = myobj.peripherals;
                                        // $api.html($api.byId("tbodyHtml"), html);
                                        //===============
                                        //遍历
                                        for (var i = 0; i < list.length; i++) {

                                            html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 12px;height :15px'>";

                                            html += "<td ><input name='sub' type='checkbox' /></td>";
                                            html += "<td name='vin' >" + list[i].uuid + "</td>";
                                            //  html += "<td name='carno' >" + list[i].rssi + "</td>";
                                            html += "<td name='seq' >" + list[i].name + "</td>";

                                            html += "<td name='sub_mrlcode' ><button  onclick = 'clickBle(\"" + list[i].uuid + "\")'>连接蓝牙</button></td>";
                                            html += "</tr>";
                                        }

                                        //console.log(html);

                                        $api.html($api.byId("tbodyHtml"), html);
                                        api.hideProgress();
                                        // ble.connect({
                                        //     peripheralUUID: "3A:CA:84:8F:B7:CC"
                                        // }, function (ret, err) {
                                        //     if (ret.status) {
                                        //         api.hideProgress();
                                        //         // alert("连接成功！");
                                        //         ble.discoverService({
                                        //             peripheralUUID: "3A:CA:84:8F:B7:CC"
                                        //         }, function (ret) {
                                        //             if (ret) {
                                        //                 //api.alert({ msg: JSON.stringify(ret) });

                                        //                 let serviices = ret.services;
                                        //                 console.log(serviices);

                                        //                 for (let item of serviices) {
                                        //                     ble.discoverCharacteristics({

                                        //                         serviceUUID: item,
                                        //                         peripheralUUID: "3A:CA:84:8F:B7:CC"

                                        //                     }, function (ret) {
                                        //                         if (ret) {
                                        //                             // api.alert({ msg: JSON.stringify(ret) });
                                        //                             console.log(JSON.stringify(ret));
                                        //                             for (let _obj of ret.characteristics) {

                                        //                                 console.log(_obj);
                                        //                                 if (_obj.properties == "18") {
                                        //                                     ble.writeValueForCharacteristic({
                                        //                                         peripheralUUID: "3A:CA:84:8F:B7:CC",
                                        //                                         serviceUUID: item,
                                        //                                         characteristicUUID: _obj.uuid,
                                        //                                         value: ''
                                        //                                     }, function (ret) {
                                        //                                         if (ret) {
                                        //                                             api.alert({ msg: JSON.stringify(ret) });
                                        //                                         }
                                        //                                     });
                                        //                                 }
                                        //                             }


                                        //                         }
                                        //                     });

                                        //                 }
                                        //                 //===========
                                        //             }
                                        //         });
                                        //     } else {
                                        //         api.hideProgress();
                                        //         alert(err.code);
                                        //     }
                                        // });


                                        //===============
                                    }
                                })
                                , 30000);

                            //===
                        }
                    })
                    , 3000)
            }
        });

    }


    //很指定的外国设备 WUID 及其服务 WUID 获族外围设备的有特《Characteristic)
    function discoverCharacteristics(services) {
        let _this = this
        for (let item of services) {
            this.ble.discoverCharacteristics({
                serviceUuID: item, //成务WID
                peripheraluUID: "3A:CA:84:8F:B7:CC" //蓝于外国设务的 UUID
            }, function (ret) {
                if (ret) (
                    console.log("获职该设备某一服务里所有的特征:", JSON.stringify(ret.characteristics))

                    // for (var i =0;i<2;i++){

                    // }

                    // for (let _obj of ret.characteristics) {
                    //     if (_obj.properties == "22") {
                    //     // model.propertiesuUID = obj.uuid //征的UID
                    //     // model.servicewUID = item //双务的UUID
                    //     // this.writeBLECharacteristicValue(ORDERSystemTime)
                    //     }
                    // }
                );
            });
        }
    }




    //返回按钮
    function closeWin() {
        var ble = api.require('ble');
        ble.clean();
        api.closeWin({});
    }
</script>

</html>